<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 标准库概览</h1>
        <h2>操作系统接口</h2>
        <p>os模块提供了不少与操作系统相关联的函数。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> os
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> os</span><span class="pun">.</span><span class="pln">getcwd</span><span
                class="pun">()</span><span class="pln">      </span><span class="com"># 返回当前的工作目录</span><span
                class="pln">
</span><span class="str">'C:\\Python34'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> os</span><span class="pun">.</span><span class="pln">chdir</span><span
                class="pun">(</span><span class="str">'/server/accesslogs'</span><span class="pun">)</span><span
                class="pln">   </span><span class="com"># 修改当前的工作目录</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> os</span><span class="pun">.</span><span class="pln">system</span><span
                class="pun">(</span><span class="str">'mkdir today'</span><span class="pun">)</span><span
                class="pln">   </span><span class="com"># 执行系统命令 mkdir </span><span class="pln">
</span><span class="lit">0</span></pre>
        <p>建议使用 "import os" 风格而非 "from os import *"。这样可以保证随操作系统不同而有所变化的 os.open() 不会覆盖内置函数 open()。
        </p>
        <p>在使用 os 这样的大型模块时内置的 dir() 和 help() 函数非常有用:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> os
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> dir</span><span class="pun">(</span><span
                class="pln">os</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&lt;</span><span class="pln">returns a list of all </span><span class="kwd">module</span><span
                class="pln"> functions</span><span class="pun">&gt;</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> help</span><span class="pun">(</span><span
                class="pln">os</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&lt;</span><span class="pln">returns an extensive manual page created </span><span class="kwd">from</span><span
                class="pln"> the </span><span class="kwd">module</span><span class="str">'s docstrings&gt;</span></pre>
        <p>针对日常的文件和目录管理任务，:mod:shutil 模块提供了一个易于使用的高级接口:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> shutil
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> shutil</span><span class="pun">.</span><span class="pln">copyfile</span><span
                class="pun">(</span><span class="str">'data.db'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'archive.db'</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> shutil</span><span class="pun">.</span><span class="pln">move</span><span
                class="pun">(</span><span class="str">'/build/executables'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'installdir'</span><span class="pun">)</span></pre>

        <h2> 文件通配符</h2>
        <p>glob模块提供了一个函数用于从目录通配符搜索中生成文件列表:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> glob
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> glob</span><span class="pun">.</span><span class="pln">glob</span><span
                class="pun">(</span><span class="str">'*.py'</span><span class="pun">)</span><span class="pln">
</span><span class="pun">[</span><span class="str">'primes.py'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'random.py'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'quote.py'</span><span class="pun">]</span></pre>

        <h2>命令行参数</h2>
        <p>通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量。例如在命令行中执行 "python demo.py one two three" 后可以得到以下输出结果:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> sys
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">sys</span><span class="pun">.</span><span
                class="pln">argv</span><span class="pun">)</span><span class="pln">
</span><span class="pun">[</span><span class="str">'demo.py'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'one'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'two'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'three'</span><span class="pun">]</span></pre>

        <h2>错误输出重定向和程序终止</h2>
        <p>sys 还有 stdin，stdout 和 stderr 属性，即使在 stdout 被重定向时，后者也可以用于显示警告和错误信息。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> sys</span><span class="pun">.</span><span class="pln">stderr</span><span
                class="pun">.</span><span class="pln">write</span><span class="pun">(</span><span class="str">'Warning, log file not found starting a new one\n'</span><span
                class="pun">)</span><span class="pln">
</span><span class="typ">Warning</span><span class="pun">,</span><span class="pln"> log file </span><span class="kwd">not</span><span
                class="pln"> found starting a </span><span class="kwd">new</span><span class="pln"> one</span></pre>
        <p>大多脚本的定向终止都使用 "sys.exit()"。</p>

        <h2>字符串正则匹配</h2>
        <p>re模块为高级字符串处理提供了正则表达式工具。对于复杂的匹配和处理，正则表达式提供了简洁、优化的解决方案:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> re
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> re</span><span class="pun">.</span><span class="pln">findall</span><span
                class="pun">(</span><span class="pln">r</span><span class="str">'\bf[a-z]*'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'which foot or hand fell fastest'</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">[</span><span class="str">'foot'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'fell'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'fastest'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> re</span><span class="pun">.</span><span
                class="kwd">sub</span><span class="pun">(</span><span class="pln">r</span><span class="str">'(\b[a-z]+) \1'</span><span
                class="pun">,</span><span class="pln"> r</span><span class="str">'\1'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'cat in the the hat'</span><span
                class="pun">)</span><span class="pln">
</span><span class="str">'cat in the hat'</span></pre>
        <p>如果只需要简单的功能，应该首先考虑字符串方法，因为它们非常简单，易于阅读和调试:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="str">'tea for too'</span><span class="pun">.</span><span class="pln">replace</span><span
                class="pun">(</span><span class="str">'too'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'two'</span><span class="pun">)</span><span class="pln">
</span><span class="str">'tea for two'</span></pre>

        <h2>数学 </h2>
        <p>math模块为浮点运算提供了对底层C函数库的访问:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> math
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> math</span><span class="pun">.</span><span class="pln">cos</span><span
                class="pun">(</span><span class="pln">math</span><span class="pun">.</span><span
                class="pln">pi </span><span class="pun">/</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">)</span><span class="pln">
</span><span class="lit">0.70710678118654757</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> math</span><span class="pun">.</span><span class="pln">log</span><span
                class="pun">(</span><span class="lit">1024</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">2</span><span class="pun">)</span><span class="pln">
</span><span class="lit">10.0</span></pre>
        <p>random提供了生成随机数的工具。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> random
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> random</span><span class="pun">.</span><span class="pln">choice</span><span
                class="pun">([</span><span class="str">'apple'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pear'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'banana'</span><span class="pun">])</span><span class="pln">
</span><span class="str">'apple'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> random</span><span class="pun">.</span><span class="pln">sample</span><span
                class="pun">(</span><span class="pln">range</span><span class="pun">(</span><span class="lit">100</span><span
                class="pun">),</span><span class="pln"> </span><span class="lit">10</span><span
                class="pun">)</span><span class="pln">   </span><span
                class="com"># sampling without replacement</span><span class="pln">
</span><span class="pun">[</span><span class="lit">30</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">83</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">16</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">8</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">81</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">41</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">50</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">18</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">33</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> random</span><span class="pun">.</span><span class="pln">random</span><span
                class="pun">()</span><span class="pln">    </span><span class="com"># random float</span><span
                class="pln">
</span><span class="lit">0.17970987693706186</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> random</span><span class="pun">.</span><span class="pln">randrange</span><span
                class="pun">(</span><span class="lit">6</span><span class="pun">)</span><span
                class="pln">    </span><span class="com"># random integer chosen from range(6)</span><span class="pln">
</span><span class="lit">4</span></pre>

        <h2>访问
            互联网</h2>
        <p>有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个是用于处理从 urls 接收的数据的 urllib.request 以及用于发送电子邮件的 smtplib:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">from</span><span class="pln"> urllib</span><span
                class="pun">.</span><span class="pln">request </span><span class="kwd">import</span><span class="pln"> urlopen
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> line </span><span
                class="kwd">in</span><span class="pln"> urlopen</span><span class="pun">(</span><span class="str">'http://tycho.usno.navy.mil/cgi-bin/timer.pl'</span><span
                class="pun">):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     line </span><span class="pun">=</span><span
                class="pln"> line</span><span class="pun">.</span><span class="pln">decode</span><span
                class="pun">(</span><span class="str">'utf-8'</span><span class="pun">)</span><span
                class="pln">  </span><span class="com"># Decoding the binary data to text.</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">if</span><span
                class="pln"> </span><span class="str">'EST'</span><span class="pln"> </span><span
                class="kwd">in</span><span class="pln"> line </span><span class="kwd">or</span><span
                class="pln"> </span><span class="str">'EDT'</span><span class="pln"> </span><span
                class="kwd">in</span><span class="pln"> line</span><span class="pun">:</span><span class="pln">  </span><span
                class="com"># look for Eastern Time</span><span class="pln">
</span><span class="pun">...</span><span class="pln">         </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">line</span><span class="pun">)</span><span class="pln">

</span><span class="pun">&lt;</span><span class="pln">BR</span><span class="pun">&gt;</span><span class="typ">Nov</span><span
                class="pun">.</span><span class="pln"> </span><span class="lit">25</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">09</span><span class="pun">:</span><span
                class="lit">43</span><span class="pun">:</span><span class="lit">32</span><span class="pln"> PM EST

</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">import</span><span class="pln"> smtplib
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> server </span><span class="pun">=</span><span
                class="pln"> smtplib</span><span class="pun">.</span><span class="pln">SMTP</span><span
                class="pun">(</span><span class="str">'localhost'</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> server</span><span class="pun">.</span><span class="pln">sendmail</span><span
                class="pun">(</span><span class="str">'soothsayer@example.org'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'jcaesar@example.org'</span><span class="pun">,</span><span
                class="pln">
</span><span class="pun">...</span><span class="pln"> </span><span class="str">"""To: jcaesar@example.org
... From: soothsayer@example.org
...
... Beware the Ides of March.
... """</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> server</span><span class="pun">.</span><span class="pln">quit</span><span
                class="pun">()</span></pre>
        <p>注意第二个例子需要本地有一个在运行的邮件服务器。</p>

        <h2>日期和时间</h2>
        <p>datetime模块为日期和时间处理同时提供了简单和复杂的方法。</p>
        <p>支持日期和时间算法的同时，实现的重点放在更有效的处理和格式化输出。
        </p>
        <p>该模块还支持时区处理:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="com"># dates are easily constructed and formatted</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">from</span><span class="pln"> datetime </span><span
                class="kwd">import</span><span class="pln"> date
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> now </span><span class="pun">=</span><span class="pln"> date</span><span
                class="pun">.</span><span class="pln">today</span><span class="pun">()</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> now
datetime</span><span class="pun">.</span><span class="pln">date</span><span class="pun">(</span><span
                class="lit">2003</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">12</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> now</span><span class="pun">.</span><span class="pln">strftime</span><span
                class="pun">(</span><span class="str">"%m-%d-%y. %d %b %Y is a %A on the %d day of %B."</span><span
                class="pun">)</span><span class="pln">
</span><span class="str">'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'</span><span class="pln">

</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="com"># dates support calendar arithmetic</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> birthday </span><span class="pun">=</span><span
                class="pln"> date</span><span class="pun">(</span><span class="lit">1964</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">31</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> age </span><span class="pun">=</span><span class="pln"> now </span><span
                class="pun">-</span><span class="pln"> birthday
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> age</span><span class="pun">.</span><span class="pln">days
</span><span class="lit">14368</span></pre>

        <h2>数据压缩</h2>
        <p>以下模块直接支持通用的数据打包和压缩格式：zlib，gzip，bz2，zipfile，以及 tarfile。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">import</span><span class="pln"> zlib
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> s </span><span class="pun">=</span><span
                class="pln"> b</span><span class="str">'witch which has which witches wrist watch'</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> len</span><span class="pun">(</span><span
                class="pln">s</span><span class="pun">)</span><span class="pln">
</span><span class="lit">41</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> t </span><span class="pun">=</span><span class="pln"> zlib</span><span
                class="pun">.</span><span class="pln">compress</span><span class="pun">(</span><span
                class="pln">s</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> len</span><span class="pun">(</span><span
                class="pln">t</span><span class="pun">)</span><span class="pln">
</span><span class="lit">37</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> zlib</span><span class="pun">.</span><span class="pln">decompress</span><span
                class="pun">(</span><span class="pln">t</span><span class="pun">)</span><span class="pln">
b</span><span class="str">'witch which has which witches wrist watch'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> zlib</span><span class="pun">.</span><span class="pln">crc32</span><span
                class="pun">(</span><span class="pln">s</span><span class="pun">)</span><span class="pln">
</span><span class="lit">226805979</span></pre>

        <h2>性能度量</h2>
        <p>有些用户对了解解决同一问题的不同方法之间的性能差异很感兴趣。Python 提供了一个度量工具，为这些问题提供了直接答案。</p>
        <p>例如，使用元组封装和拆封来交换元素看起来要比使用传统的方法要诱人的多,timeit 证明了现代的方法更快一些。</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">from</span><span class="pln"> timeit </span><span class="kwd">import</span><span
                class="pln"> </span><span class="typ">Timer</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="typ">Timer</span><span
                class="pun">(</span><span class="str">'t=a; a=b; b=t'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'a=1; b=2'</span><span class="pun">).</span><span class="pln">timeit</span><span
                class="pun">()</span><span class="pln">
</span><span class="lit">0.57535828626024577</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="typ">Timer</span><span
                class="pun">(</span><span class="str">'a,b = b,a'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'a=1; b=2'</span><span class="pun">).</span><span class="pln">timeit</span><span
                class="pun">()</span><span class="pln">
</span><span class="lit">0.54962537085770791</span></pre>
        <p>相对于 timeit 的细粒度，:mod:profile 和 pstats 模块提供了针对更大代码块的时间度量工具。</p>

        <h2>测试模块</h2>
        <p>开发高质量软件的方法之一是为每一个函数开发测试代码，并且在开发过程中经常进行测试
        </p>
        <p>doctest模块提供了一个工具，扫描模块并根据程序中内嵌的文档字符串执行测试。
        </p>
        <p>测试构造如同简单的将它的输出结果剪切并粘贴到文档字符串中。
        </p>
        <p>通过用户提供的例子，它强化了文档，允许 doctest 模块确认代码的结果是否与文档一致:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">def</span><span
                class="pln"> average</span><span class="pun">(</span><span class="pln">values</span><span
                class="pun">):</span><span class="pln">
    </span><span class="str">"""Computes the arithmetic mean of a list of numbers.

    &gt;&gt;&gt; print(average([20, 30, 70]))
    40.0
    """</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> sum</span><span class="pun">(</span><span class="pln">values</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">/</span><span
                class="pln"> len</span><span class="pun">(</span><span class="pln">values</span><span
                class="pun">)</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> doctest
doctest</span><span class="pun">.</span><span class="pln">testmod</span><span class="pun">()</span><span
                class="pln">   </span><span class="com"># 自动验证嵌入测试</span></pre>
        <p>unittest模块不像 doctest模块那么容易使用，不过它可以在一个独立的文件里提供一个更全面的测试集:
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="kwd">import</span><span class="pln"> unittest

</span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestStatisticalFunctions</span><span
                class="pun">(</span><span class="pln">unittest</span><span class="pun">.</span><span class="typ">TestCase</span><span
                class="pun">):</span><span class="pln">

    </span><span class="kwd">def</span><span class="pln"> test_average</span><span class="pun">(</span><span
                class="kwd">self</span><span class="pun">):</span><span class="pln">
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">assertEqual</span><span
                class="pun">(</span><span class="pln">average</span><span class="pun">([</span><span
                class="lit">20</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">30</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">70</span><span class="pun">]),</span><span class="pln"> </span><span class="lit">40.0</span><span
                class="pun">)</span><span class="pln">
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">assertEqual</span><span
                class="pun">(</span><span class="pln">round</span><span class="pun">(</span><span
                class="pln">average</span><span class="pun">([</span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">7</span><span class="pun">]),</span><span
                class="pln"> </span><span class="lit">1</span><span class="pun">),</span><span class="pln"> </span><span
                class="lit">4.3</span><span class="pun">)</span><span class="pln">
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">assertRaises</span><span
                class="pun">(</span><span class="typ">ZeroDivisionError</span><span class="pun">,</span><span
                class="pln"> average</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">[])</span><span class="pln">
        </span><span class="kwd">self</span><span class="pun">.</span><span class="pln">assertRaises</span><span
                class="pun">(</span><span class="typ">TypeError</span><span class="pun">,</span><span class="pln"> average</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">20</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">30</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">70</span><span class="pun">)</span><span class="pln">

unittest</span><span class="pun">.</span><span class="pln">main</span><span class="pun">()</span><span
                class="pln"> </span><span class="com"># Calling from the command line invokes all tests</span></pre>

    </div>
</div>
</body>
</html>
